﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
using Albon.Model;

namespace DAL
{
    public class CookieDAL
    {
        //CookieInfo

        /// <summary>
        /// 从数据库中获取cookie信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>

        public DataSet getCookie(string name)
        {
            string cmdstr = "select * from cookieInfo where username='"+name+"'";
            DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
            return ds;
        }
        
        /// <summary>
        /// 向数据库中添加cookie信息
        /// </summary>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public int alterCookie(CookieInfo cookie)
        {
            SqlParameter[] parameters = {
                    
					new SqlParameter("@UserName", SqlDbType.VarChar,50),
                    new SqlParameter("@computerName",SqlDbType.VarChar,50),
					new SqlParameter("@regName", SqlDbType.VarChar,50),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Passed", SqlDbType.Bit,1),
					new SqlParameter("@Browser", SqlDbType.VarChar,50),
                    new SqlParameter("@id",SqlDbType.Int,4)};
			parameters[0].Value = cookie.UserName;
            parameters[1].Value = cookie.Computername;
			parameters[2].Value = cookie.regName;
			parameters[3].Value = cookie.Email;
			parameters[4].Value = cookie.CreateDate;
			parameters[5].Value = cookie.Passed;
			parameters[6].Value = cookie.Browser;
            parameters[7].Value = cookie.ID;
            return SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "p_cookiealter", parameters);
        }
        /// <summary>
        /// 如果删除的数据条目和传入的id个数相等，则返回True
        /// </summary>
        /// <param name="idlist"></param>
        /// <returns></returns>
        public int delCookie(string idlist)
        {
            string cmdstr = "delete from CookieInfo where id in ("+idlist+")";
            int ret=SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null);
            return ret;
        }


        /// <summary>
        /// 修改cookie信息的状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        public void setState(int id,bool state)
        {
            string cmdstr = "update CookieInfo set passed='"+state+"' where id="+id.ToString();
            SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }

    }
}
